//
//  JSMHistoryTool.m
//  TakeYouTourTogether
//
//  Created by dllo on 15/12/2.
//  Copyright © 2015年 king. All rights reserved.
//

#import "JSMHistoryTool.h"

@implementation JSMHistoryTool
+ (instancetype)historyBaseHandle{
    static JSMHistoryTool *single;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        single = [[JSMHistoryTool alloc]init];
    });
    return single;
}

#pragma mark 开启数据库
- (void)openDB{
    NSString *sandPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    //拼接路径
    NSString *dbpath = [sandPath stringByAppendingPathComponent:@"searchhistory.sqlite"];
    int result = sqlite3_open([dbpath UTF8String], &dbPoint);
    if (result == SQLITE_OK) {
        NSLog(@"开启成功");
    }else {
        NSLog(@"开启失败");
    }
    NSLog(@"%@", dbpath);
}
#pragma mark 创建数据表
- (void)createTable{
    NSString *sqlStr = @"create table if not exists searchhistory (history_name text, history_des text, history_id text)";
    int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"创建成功");
    }else{
        NSLog(@"创建失败");
    }
}

#pragma mark 查找是否存在
- (SearchHistory)isHaveHistory:(JSMTouringModel *)h_model{
    NSString *sqlStr = [NSString stringWithFormat:@"select * from searchhistory where history_name = '%@' ", h_model.name];
    //创建跟随指针
    sqlite3_stmt *stmt = nil;
    
    //执行SQL语句
    int result = sqlite3_prepare_v2(dbPoint, [sqlStr UTF8String], -1, &stmt, nil);
    NSLog(@"%d", result);
    
    if (result == SQLITE_OK) {
        NSLog(@"查询成功");
        
        if (sqlite3_step(stmt) == SQLITE_ROW) {
            sqlite3_finalize(stmt);
            return InTable;
        }else{
            sqlite3_finalize(stmt);
            NSLog(@"不在");
            return NotInTable;
        }
    }else{
        NSLog(@"查询失败");
        sqlite3_finalize(stmt);
        return SelectError;
    }
}

#pragma mark - 收藏
- (void)saveHistory:(JSMTouringModel *)h_model{
    NSString *sqlStr = [NSString stringWithFormat:@"insert into searchhistory (history_name, history_des, history_id) values ('%@', '%@', '%@' )", h_model.name, h_model.destination, h_model.touringId];
    
    int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil);
    NSLog(@"%d", result);

    if (result == SQLITE_OK) {
        
        NSLog(@"记录成功");
    } else{
        NSLog(@"记录失败");
    }

}

#pragma mark 删除
- (void)deleteHistory:(JSMTouringModel *)h_model{
     NSString *sqlStr = [NSString stringWithFormat:@"delete from searchhistory where history_name = '%@'", h_model.name];
    int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"删除成功");
    }else {
        NSLog(@"删除失败");
    }

}

#pragma mark 显示所有
- (NSMutableArray *)selectAllHistory{
    NSMutableArray *his_Arr = [NSMutableArray array];
    
     NSString *sqlStr = @"select * from searchhistory";
    
    sqlite3_stmt *stmt = nil;
    
    int result = sqlite3_prepare_v2(dbPoint, [sqlStr UTF8String], -1, &stmt, nil);
    
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            JSMTouringModel *model = [[JSMTouringModel alloc]init];
            model.name = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 0)];
            model.destination = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 1)];
            model.touringId = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 2)];
            [his_Arr addObject:model];
        }
    }
    sqlite3_finalize(stmt);
    return his_Arr;

}

@end
